package org.example.testcode.controller;


import com.alibaba.fastjson.JSON;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.example.testcode.FactoryBean.MyFactoryBean;
import org.example.testcode.bean.EsGoodsInfo;
import org.example.testcode.helper.EsTemplate;
import org.example.testcode.helper.IkAnalzyerUtil;
import org.example.testcode.helper.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.matchQuery;

@RestController
public class Test1Controller {

    @Autowired
    private MyFactoryBean user;

    @Autowired
    EsTemplate esTemplate;

    @GetMapping("/testService1")
    public String testService1() throws Exception {
        return user.getObject().toString();
    }


    @GetMapping("/createIndex")
    public boolean createIndex() throws IOException {
        boolean test = esTemplate.createIndex("test");
        return test;
    }

    @GetMapping("/addIndex")
    public boolean addIndex() throws IOException {

        List<EsGoodsInfo> list = new ArrayList<>();

        for (int i = 10; i < 15; i++) {
            EsGoodsInfo esGoodsInfo = new EsGoodsInfo();
            esGoodsInfo.setGoodsInfoId(Long.valueOf(i + 1));
            esGoodsInfo.setGoodsInfoName("语文，" + (i + 1));
            list.add(esGoodsInfo);
        }

        esTemplate.bulkAdd("test", EsGoodsInfo.class.getTypeName(), list);


        return true;
    }

    @GetMapping("/s")
    public String searchHits(String keywords) {
        if (!(null != keywords && keywords.length() > 0)) {
            keywords = "好好学习，天天向上";
        }
        Object object = esTemplate.search("test", keywords);

        return JSON.toJSONString(object);
    }


    @GetMapping("/search")
    public String search(String keywords) {
        //封装查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if (null != keywords && keywords.length() > 0) {
            List<String> analyzedWords = IkAnalzyerUtil.segmentPhraseByIk(keywords);
            for (String word : analyzedWords) {
//                boolQuery.must(queryStringQuery(String.format("\"*%s*\" OR *%s*", word.trim(), word.trim())).field("goodsInfoName").field("goodsInfoName")
//                        .field("goodsInfoName").field("goodsInfoName"));

                /*boolQuery.must(queryStringQuery(String.format("\"*%s*\" OR *%s*", word.trim(),
                        word.trim())).field("goodsInfoName")
                        .field("goodsInfoName"));*/

                /*boolQuery.must(queryStringQuery(String.format("*", word.trim(),
                        word.trim())).field("goodsInfoName"));*/

                boolQuery.must(matchQuery("goodsInfoName", word));
            }
        }

        searchSourceBuilder.query(boolQuery);

        int pageNo = 1;
        int pageSize = 10;

        searchSourceBuilder.from((pageNo - 1) * pageSize).size(pageSize);


        PageResult<?> searchResult = esTemplate.search("test",
                EsGoodsInfo.class, searchSourceBuilder);


        System.out.println("searchResult:" + JSON.toJSONString(searchResult));


        Object object = esTemplate.search("test", keywords);

        return JSON.toJSONString(searchResult);

    }

}